<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    const eg = [
        {
            value1: [2, 4, 3],
            value2: [5, 6, 4],
            res: [7, 0, 8]
        },
        // {
        //     value1: [0],
        //     value2: [0],
        //     res: [0]
        // },
        // {
        //     value1: [9,9,9,9,9,9,9],
        //     value2: [9,9,9,9],
        //     res: [8,9,9,9,0,0,0,1]
        // }
    ];

    const arrayToListNode = (arr) => {

        const node = {
            val: arr[0],
            next: {},
        };
        const len = arr.length - 1;
        let activeNext = node.next;
        let i = 1;
        while (i <= len) {
            if (arr[i] !== undefined) {
                if (i === len) {
                    activeNext.next = null;
                } else {
                    activeNext.next = {
                        val: null,
                        next: null,
                    };
                }
                activeNext.val = arr[i];
                activeNext = activeNext.next
            }
            i++;
        }
        return node;
    };

    const listNodeToArray = (listNode) => {
        const res = [];
        let activeNode = listNode;
        while (activeNode.next) {
            res.push(activeNode.val);
            activeNode = activeNode.next;
            if (activeNode.next === null) {
                res.push(activeNode.val);
            }
        }
        return res;
    }

    const fn1 = (l1, l2) => {
        const completeTheLength = (_value1, _value2) => {
            const len1 = _value1.length;
            const len2 = _value2.length;
            if (len1 === len2) {
                return {
                    newValue1: _value1,
                    newValue2: _value2,
                }
            }
            let completeValue = len1 > len2 ? _value2 : _value1;
            const addLen = Math.abs(len1 - len2);
            const addValue = new Array(addLen).fill(0);
            completeValue.unshift(...addValue);
            return {
                newValue1: _value1.reverse(),
                newValue2: _value2.reverse(),
            }
        };
        const {newValue1, newValue2} = completeTheLength(l1, l2);
        const addFun = (_v1, _v2) => {
            const len = _v1.length;
            let isAdd = false;
            const res = [];
            for (let index = 0; index < len; index++) {
                const addTemp = isAdd ? _v1[index] + _v2[index] + 1 : _v1[index] + _v2[index];
                isAdd = false
                if (addTemp > 9) {
                    isAdd = true;
                    res.push(addTemp - 10);
                } else {
                    res.push(addTemp);
                }
            }
            if (isAdd) {
                res.push(1);
            }
            return res
        };

        return addFun(newValue1, newValue2);
    }
    const run = (egList) => {
        const fnList = [fn1];
        const errList = [];
        const resultArr = fnList.map((fnItem, index) => {
            console.time(`fn${index + 1}`);
            let res = true;
            egList.forEach((egItem, index) => {
                const v1 = arrayToListNode(egItem.value1);
                console.log(v1);
                listNodeToArray(v1);
                if (egItem.res.join(',') !== fnItem(egItem.value1, egItem.value2)?.join(',')) {
                    res = false;
                    errList.push(index)
                }
            })
            console.timeEnd(`fn${index + 1}`);
            return res;
        })
        return {
            state: !resultArr.includes(false),
            errList
        };
    };

    const init = () => {

        const {state, errList} = run(eg);
        if (state) {
            console.log('全量通过');
        } else {
            console.log('实例未通过', errList)
        }
    };

    init();
</script>
</html>
